package com.egoo.ticket.server.service.impl;

import com.egoo.ticket.server.dao.TaskSignInMapper;
import com.egoo.ticket.server.pojo.entity.TaskSignIn;
import com.egoo.ticket.server.pojo.vo.TaskSignInVo;
import com.egoo.ticket.server.service.TaskSignInService;
import com.egoo.ticket.server.utils.activiti.ActTaskServiceUtil;
import com.egoo.ticket.server.utils.common.CreateCodeUtil;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class TaskSignInServiceImpl implements TaskSignInService {

	@Autowired
	private TaskSignInMapper taskSignInMapper;
	@Autowired
	private CreateCodeUtil createCodeUtil;
	/**
	 * TODO 根据条件获取任务签到记录
	 * @author: victor_tang
	 * @createtime: 2020/2/17 18:37
	 * @param processInstanceId 1
	 * @return: java.utils.List<com.egoo.ews.service.vo.TaskSignInVo>
	 */
	@Override
	public TaskSignInVo getTaskSignInByProcessInstanceId(String processInstanceId, String tenantId){
		String taskId = ActTaskServiceUtil.getActiveTaskIdByProcessInstId(processInstanceId);
		if (StringUtils.isEmpty(taskId)) {
			log.error("任务id获取失败！");
			return null;
		}
		Map<String, Object> seachMap = new HashMap<>(4);
		seachMap.put("taskId",taskId);
		seachMap.put("tenantId",tenantId);
		List<TaskSignInVo> taskSignInVos = taskSignInMapper.selectTaskSignInListByCondition(seachMap);
		TaskSignInVo taskSignInVo = new TaskSignInVo();
		if (taskSignInVos!=null&&taskSignInVos.size()>0) {
			taskSignInVo = taskSignInVos.get(0);
			// 组装签到时间和签到地址信息
			taskSignInVo.setSignInAddress(handleTaskInfoAddress(taskSignInVos));
		}
		return taskSignInVo;
	}
	/**
	 * TODO 拼接任务签到信息
	 * @author: victor_tang
	 * @createtime: 2020/4/10 14:30
	 * @param taskSignInVos 1
	 * @return: java.lang.String
	 */
	private String handleTaskInfoAddress(List<TaskSignInVo> taskSignInVos){
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (taskSignInVos==null || taskSignInVos.size()==0) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (TaskSignInVo taskSignInVo : taskSignInVos) {
			sb.append(sdfTime.format(taskSignInVo.getSignInTime()))
					.append("<br>")
					.append(taskSignInVo.getSignInAddress())
					.append("<br>");
		}
		return sb.toString();
	}
	/**
	 * TODO 新增或者编辑任务签到
	 * @author: victor_tang
	 * @createtime: 2020/2/17 18:38
	 * @param taskSignIn 1
	 * @return: void
	 */
	@Override
	public DbResponse addOrUpdateTaskSignIn(TaskSignIn taskSignIn) {
		taskSignIn.setSignInTime(new Date());
		int result = 0;
		if (StringUtils.isEmpty(taskSignIn.getId())) {
			// 新增签到
			taskSignIn.setId(createCodeUtil.getTaskSignInId());
			String taskId =
					ActTaskServiceUtil.getActiveTaskIdByProcessInstId(taskSignIn.getProcessInstanceId());
			taskSignIn.setTaskId(taskId);
			result = taskSignInMapper.insertSelective(taskSignIn);

		}else{
			// 更新签到信息
			result = taskSignInMapper.updateByPrimaryKeySelective(taskSignIn);
		}

		if (result>0) {
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"签到成功！");
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,"签到失败！");
	}
}
